Working With Calendar in Window Phone 7


Introduction

In this article we are going to see how to use the Calendar application to access the calendar data and use it across the development as and when needed. In order to use the calendar object we are going to use the reference of the Appointments object and do an Async search to get the results and show it on to a collection of Appointment objects. The end result in the appointments object can be used in different ways as per the requirement by binding to different controls and to do some schedule. Appointment class is to interact with user appointment data and is inherited from the Microsoft.Phone.UserData namespace. Let us see the step by step process on how to use the calendar objects and list the appointments.

Steps:

Open Visual Studio 2010 and create a new Silverlight for Windows Phone 7 application with a valid project name as shown in the screen below.

image1.gif

Now in order to fetch the details of the calendar appointments and show it to the end users, we will add controls from the tools as shown in the screen below. We can just drag and drop the controls from the tool box or write the XAML code as shown in the screen below or copy paste the XAML code.

XAML Code:

<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
    <TextBlock x:Name="ApplicationTitle" Text="F5DEBUG WP7 TUTORIALS" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock x:Name="PageTitle" Text="calendar" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>
<!--ContentPanel - place additional content here-->

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
 <Button Content="Get Appointment" Height="83" HorizontalAlignment="Left" Margin="30,16,0,0" 
Name="btnAppointment"  VerticalAlignment="Top" Width="402" Click="btnAppointment_Click" 
/>
 <ListBox Name="lstAppointment" ItemsSource="{Binding}" Margin="47,188,36,52" >
 <ListBox.ItemTemplate>
 <DataTemplate>
 <TextBlock Name="txtResults" Text="{Binding Path=DisplayName, Mode=OneWay}" />
</DataTemplate>
</
ListBox.ItemTemplate>
</
ListBox>
<
TextBlock Height="41" HorizontalAlignment="Left" Margin="47,118,0,0" Name="txtAppointments" 
Text="List of Appointments" VerticalAlignment="Top" Width="373" 
/>
</Grid> 
</Grid>

 

img2.png

Now we need to add code to do listing of the calendar appointments to the list view. First add the namespace

using Microsoft.Phone.UserData;

Now add the button click event code, here we are going to create an object of the Appointment class and do an AsyncSearch. We need to provide the start time and end time to fetch the appointment details and also should provide the maximum count. Copy and paste the below code to the code behind for the button click event.

C# Code:

private void btnAppointment_Click(object sender, RoutedEventArgs e)

{

     Appointments aAppointment = new Appointments();

     aAppointment.SearchCompleted += new EventHandler<AppointmentsSearchEventArgs>(GetAppointments);

     DateTime starttime = DateTime.Now;

     DateTime endtime = starttime.AddDays(10);

     int maxAppointment = 20;

     aAppointment.SearchAsync(starttime, endtime, maxAppointment, null);

}

void GetAppointments(object sender, AppointmentsSearchEventArgs e)

{

    try

    {

         lstAppointment.DataContext = e.Results;

    }

    catch (System.Exception)

   {

        txtAppointments.Text = "No Appointments Found!!!";

   }

   if (lstAppointment.Items.Any())

   {

       txtAppointments.Text = "Below is the List of Appointments";

   }

   else

   {

        txtAppointments.Text = "No Appointments Found!!!";

   }

}

 

Now the above code will pull the information from the appointments object and bind it to the data context of the list box. If there is no appointments available we can see an empty result as well. To check the application just run the application by pressing F5 directly from the keyboard or by selecting build and execute solution from the tool bar and we can see the screen as shown in the screen below.

img3.png

Since we are in the emulator and we don't have any appointments saved the list is empty. If any appointments are saved in the physical device we will get the complete list of the next 10 days to be displayed here in the list box.

Conclusion

So in this article we have seen how to use the calendar object to fetch the appointment details and list it out. We can customize to use the appointments object to bind to different data binding options as per the requirements.

Up Next
    Ebook Download
    View all
    Learn
    View all